﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Model;
using System.Web.Mvc;
using Shop.Framework.Keys;

namespace Shop.Framework.Control {
    public class CartControl : ViewUserControl {

        public List<OrderItem> OrderItems { get; set; }
        public int ItemCount {
            get {
                return DeterminCount();
            }
        }

        private int DeterminCount() {
            if (OrderItems.Count == 0)
                return 0;
            int total = 0;
            OrderItems.ForEach(delegate(OrderItem item) {
                total += item.Quantity;
            });

            return total;
        }

        protected override void OnInit(EventArgs e) {
            try {
                OrderItems = Session[SessionKey.OrderItems] as List<OrderItem>;
                if (OrderItems == null) {
                    OrderItems = new List<OrderItem>();
                    Session[SessionKey.OrderItems] = OrderItems;
                }
                base.OnInit(e);
            } catch (Exception ex) {
                throw ex;
            }
        }

    }
}
